//
//  FuelMainViewController.m
//  carcareIOS
//
//  Created by ileo on 16/4/11.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "FuelMainViewController.h"
#import "FuelStatView.h"
#import "OilMgrDetailCell.h"

#define TOP_HEIGHT_MAX 282
#define TOP_HEIGHT_MIN 106

@interface FuelMainViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) FuelStatView *fuelStatView;
@property (nonatomic, strong) FuelMgr *fuelMgr;
@property (nonatomic , strong) UITableView * tableView;

@property (nonatomic , strong) FuelMonthModel * model;

@property (nonatomic, assign) UIView *noDataView;

@end

@implementation FuelMainViewController

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateFuelMonthInfo) name:FUEL_UPDATE object:nil];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, TOP_HEIGHT_MIN, __MAIN_WIDTH, __MAIN_HEIGHT_64 - TOP_HEIGHT_MIN) style:UITableViewStylePlain];
    self.tableView.contentInset = UIEdgeInsetsMake(TOP_HEIGHT_MAX - TOP_HEIGHT_MIN, 0, 100, 0);
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorColor = [UIColor clearColor];
    [self.view addSubview:self.tableView];
    
    self.noDataView = [[[UIView viewWithFrame:CGRectMake(0, 0, __MAIN_WIDTH, __MAIN_HEIGHT_64 - TOP_HEIGHT_MIN)] resetBackgroundColor:[UIColor whiteColor]] setupOnView:self.tableView];
    [Factory labelWithFrame:CGRectMake(0, self.tableView.height/2 - self.tableView.contentInset.top + 20, __MAIN_WIDTH, 20) font:FONT_DEFAULT_Light(14) text:@"暂无加油纪录" textColor:[Theme wordGrayColor] onView:self.noDataView textAlignment:NSTextAlignmentCenter];
    [Factory labelWithFrame:CGRectMake(0, self.tableView.height/2 - self.tableView.contentInset.top + 40, __MAIN_WIDTH, 20) font:FONT_DEFAULT_Light(14) text:@"点击＋号可以手动添加" textColor:[Theme wordGrayColor] onView:self.noDataView textAlignment:NSTextAlignmentCenter];
    self.noDataView.hidden = YES;
    
    __weak __typeof(self) wself = self;
    self.fuelStatView.frame = CGRectMake(0, 0, __MAIN_WIDTH, TOP_HEIGHT_MAX);
    self.fuelStatView.scrollChangeMonthReload = ^()
    {
        [wself updateFuelMonthInfo];
    };
    [self.fuelStatView showMonth:self.fuelMgr.currentMonth];

#pragma mark - 添加按钮
    UIButton * addBtn = [Factory buttonWithCenter:CGPointMake(__MAIN_WIDTH / 2, __MAIN_HEIGHT_64 - 10 * __MAIN_RATIO_375 - ImageNamed(@"addOilBtnImage").size.height / 2) withImage:ImageNamed(@"addOilBtnImage") click:^{
        [[StatisticsMgr sharedInstance] addClickID:@"加油记录-新增加油记录按钮" description:addBtn.description];
        if(wself.addBtnClick)wself.addBtnClick();
    } onView:self.view];
    
    // Do any additional setup after loading the view.
}
-(void)updateFuelMonthInfo{
    self.model = [[FuelMgr sharedInstance]fuelModelWithMonthModel:[FuelMgr sharedInstance].currentMonth];
    if(self.model.fuelRecords.count > 0){
        [self.tableView reloadData];
        self.noDataView.hidden = YES;
    }else{
        [self.tableView reloadData];
        self.noDataView.hidden = NO;
    }
}
#pragma mark - scroll delegate

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (self.tableView.contentOffset.y >= TOP_HEIGHT_MIN - TOP_HEIGHT_MAX && self.tableView.contentOffset.y <= 0) {
        self.fuelStatView.height = TOP_HEIGHT_MIN - self.tableView.contentOffset.y;
    }else if (self.tableView.contentOffset.y < TOP_HEIGHT_MIN - TOP_HEIGHT_MAX){
        self.fuelStatView.height = TOP_HEIGHT_MAX;
    }else if (self.tableView.contentOffset.y > 0){
        self.fuelStatView.height = TOP_HEIGHT_MIN;
    }
}
#pragma mark - tableView delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.model.fuelRecords.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [OilMgrDetailCell getCellHeight];
}
- (UITableViewCell * )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * cellID = @"cellID";
    OilMgrDetailCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
         cell = [[OilMgrDetailCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }

    cell.model = self.model.fuelRecords[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [[StatisticsMgr sharedInstance] addClickID:@"加油记录-点击查看加油记录详情" description:[tableView cellForRowAtIndexPath:indexPath].description];
    FuelRecordModel * recordModel = self.model.fuelRecords[indexPath.row];
    if(self.cellSelectClick)self.cellSelectClick(recordModel);
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(FuelStatView *)fuelStatView{
    if (!_fuelStatView) {
        _fuelStatView = [[FuelStatView alloc] init];
        [_fuelStatView configMax:TOP_HEIGHT_MAX min:TOP_HEIGHT_MIN];
        [self.view addSubview:_fuelStatView];
    }
    return _fuelStatView;
}

-(FuelMgr *)fuelMgr{
    if (!_fuelMgr) {
        _fuelMgr = [FuelMgr sharedInstance];
    }
    return _fuelMgr;
}
- (void)reloadTabelViewData
{
    [self updateFuelMonthInfo];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

}
@end
